package com.xm2.demo.system.order.controller;

import com.xm2.demo.model.*;
import com.xm2.demo.system.order.ov.OrderPage;
import com.xm2.demo.system.order.service.OrderService;
import com.xm2.demo.utils.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

/**
 * @Create
 * @create 2018-12-12 16:31
 * @Description：订单控制器
 */
@Controller
@RequestMapping(value = "/system/")
public class OrderController {
    @Autowired
    @Qualifier("orderService")
    private OrderService orderService;
    /*    
     *  进入 订单页面
     * @return java.lang.String
     *       
     */ 
    @GetMapping(value = "order")
    public String order(Model model, HttpSession httpSession) throws ParseException {
        Pagination pagination = new Pagination();
        pagination = orderService.findByOrderPagination(pagination);
        Position position = (Position) httpSession.getAttribute("position");
        List<State> statusList = orderService.findStatus(position.getStateArr());
        model.addAttribute("pagination",pagination);
        model.addAttribute("statusList",statusList);
        return "order-list";
    }
    /**    
     * 条件分页查询
     * @return java.lang.String
     *       
     */ 
    @PostMapping(value = "order")
    @ResponseBody
    public Pagination doSelectOrder(@RequestBody OrderPage orderPage) throws ParseException {
        // 根据时间分页
        if(orderPage.getStartDate() == "" && orderPage.getStopDate() == "" && orderPage.getOrderId() == ""){
         return orderService.findByOrderPagination(orderPage);
        }else if(orderPage.getStartDate() != "" || orderPage.getStopDate() != "" && orderPage.getOrderId()== ""){
          return orderService.findByDatePagination(orderPage);
        }else if(orderPage.getOrderId()!= ""){
         return orderService.findByOrderId(orderPage);
        }
        return null;
    }
    /**    
     *  添加订单
     * @param stateRecord  洗衣表记录表
     * @return java.lang.String
     *       
     */ 
    @PostMapping(value = "addOrder")
    @ResponseBody
    public String addOrder(@RequestBody StateRecord stateRecord){
        try{
        orderService.addOrder(stateRecord);
        return "SUCCESS";
        }catch (Exception e){
            return "fail";
        }
    }
    /**    
     *  修改订单状态
     * @param stateRecord  订单信息
     * @return java.lang.String
     *       
     */ 
    @PostMapping(value = "updateOrder")
    @ResponseBody
    public String updateOrder(@RequestBody StateRecord stateRecord,HttpSession session){
        try{
            User user = (User)session.getAttribute("admin");
            orderService.updateOrder(stateRecord,user.getName(),user.getPhone());
            return "SUCCESS";
        }catch (Exception e){
            return "fail";
        }
    }

    @PostMapping(value = "deleteOrder")
    @ResponseBody
    public String deleteOrder(@RequestBody OrderPage orderPage){
        try{
            orderService.deleteOrder(orderPage.getIds());
            return "SUCCESS";
        }catch (Exception e){
            return "fail";
        }
    }
}
